package com.yzj.android.dao.impl;

import com.yzj.Bean.Cake;
import com.yzj.Bean.Cart;
import com.yzj.android.dao.BaseDao;
import com.yzj.android.dao.CakeDao;
import com.yzj.android.dao.CartDao;

import java.math.BigDecimal;
import java.util.List;

public class CartDaoImpl extends BaseDao implements CartDao {
    @Override
    public int addItems(Cart cart) {
        String sql =" INSERT INTO cart(`user_id`,`c_id`,`c_name`,`c_price`,`c_count`,`c_img`) VALUE(?,?,?,?,?,?)";
        return update(sql,cart.getUser_id(),cart.getC_id(),cart.getC_name(),cart.getC_price(),cart.getC_count(),cart.getC_img());
    }

    @Override
    public int deleteById(Integer id, String user_id) {
        String sql = "delete from cart where c_id = ? and user_id = ?";
        return update(sql, id,user_id);
    }

    @Override
    public int updateItems(Cart cart) {
        System.out.println(cart.getUser_id()+" "+ cart.getC_id());
        String sql = "update cart set `c_count`=?  where `c_id` = ? and `user_id` = ?";
        return update(sql,cart.getC_count()+1,cart.getC_id(),cart.getUser_id());
    }

    @Override
    public List<Cart> queryItems(String user_id) {
        String sql = "select *,`c_price`*`c_count` single_price from cart where `user_id` = ?";
        return queryForList(Cart.class, sql,user_id);
    }

    @Override
    public int queryTotalCunt(String user_id) {
        String sql = "SELECT SUM(c_count) FROM cart where user_id=?  group by user_id ";
        Number count = (Number) queryForSingleValue(sql,user_id);
        return count.intValue();
    }

    @Override
    public BigDecimal queryTotalPrice(String user_id) {
        String sql = "SELECT SUM( c_count*c_price)  FROM cart  where user_id = ? group by user_id" ;
        BigDecimal count = (BigDecimal) queryForSingleValue(sql,user_id);
        return count;
    }

    @Override
    public BigDecimal querySingePrice(String user_id) {
        String sql = "SELECT c_count*c_price  FROM cart where user_id = ? ";
        BigDecimal count = (BigDecimal) queryForSingleValue(sql,user_id);
        return count;
    }

    @Override
    public Cart queryItem(String id, String user_id) {
        String sql ="SELECT `user_id`,`c_id`,`c_name`,`c_price`,`c_count`,`c_img` FROM `cart` WHERE `c_id`=? and `user_id`=?";
        return queryForOne(Cart.class,sql,id,user_id);
    }
}
